
function! s:python3_setup()
    if has('python3')
        py3 import vim
        py3 import sys
        py3 print(sys.executable)
    else
        if filereadable($PYTHON_EXE) && executable($PYTHON_EXE)
            let g:python3_host_prog = $PYTHON_EXE
        else
            let g:python3_host_prog = get(g:, "python3_host_prog", exepath('python'))
        endif
        if executable(g:python3_host_prog)
            let &pythonthreehome = fnamemodify(g:python3_host_prog, ':p:h')
            let $PATH = &pythonthreehome .. ";" .. $PATH
            call assert_false(empty(globpath($PATH, &pythonthreehome, 1, 1)), "pythonthreehome not in path")
        endif
        if has("win32")
            if filereadable($PYTHON_DYNAMIC) && executable($PYTHON_DYNAMIC)
                let &pythonthreedll = $PYTHON_DYNAMIC
            else
                    let extension = "dll"
                    let filename = printf("python3*.%s", extension)
                    let filepath = globpath(&pythonthreehome, filename, 1, 1)
                    if !empty(filepath)
                        call assert(type(filepath) == v:t_list)
                        if len(filepath) > 0 && executable(filepath)
                            let &pythonthreedll = filepath[-1]
                        endif
                    endif
            endif
        endif
        return get(g:, "python3_host_prog", "")
    endif
endfunction

function! api#pyenv#init()
    return s:python3_setup()
endfunction

function! api#pyenv#request(method, url, options) abort
    let result = {}
    if executable(s:python3_setup())
        py3 import requests
        py3 method = getattr(requests, vim.eval('a:method'))
        py3 url = vim.eval("a:url")
        py3 kwargs = vim.eval("a:options")
        py3 response = method(url, kwargs)
        py3 result = { "status_code": response.status_code, "content": response.content }
        let result = py3eval("result")
    endif
    return result
endfunction

" vim: ts=4 sw=4 fdm=indent
